angular.module('app').controller('detail', ['$log', '$scope', '$http', '$q', '$state', 'cache', ($log, $scope, $http, $q, $state, cache) => {
  $scope.isLogin = !!cache.get('name');
  $scope.message = $scope.isLogin ? '投个简历' : '去登录';
  // 获取详细信息 返回一个promise对象
  function getDetail() {
    var def = $q.defer();
    $http.get('data/position.json', {
      params: {
        id: $state.params.id
      }
    }).success((res) => {
      $scope.detailData = res;
      if (res.posted) {
        $scope.message = '已投递'
      }
      // else {
      //   $scope.message = '去投递'
      // }
      def.resolve(res);
    }).error((err) => {
      def.reject(err)
    });
    return def.promise;
  }
  // 获取公司信息
  function getCompany(id) {
    $http.get('data/company.json?id=' + id).success(function (resp) {
      $scope.company = resp;
    })
  }
  getDetail().then((res) => {
    getCompany(res.companyId);
  });
  $scope.go = function () {
    if ($scope.message !== '已投递') {
      if ($scope.isLogin) {
        $http.post('data/handle.json', {
          id: $scope.detailData.id
        }).success(function (resp) {
          $log.info(resp);
          $scope.message = '已投递';
        });
      } else {
        $state.go('login');
      }
    }
  }
}])
